$(document).ready(function() {
	// 
	$.fn.serializeObject = function(){
		var obj = {};
		$.each(this.serializeArray(), function(index){
			var v = this['value'];
			try{
				var t = JSON.parse(v);
				if(typeof(t) == 'object') {
					v = t;
				}
			}catch(e){}
		   if(this['name'].indexOf('.') > -1) {
			   var n = this['name'].split('.');
			   if(!obj[n[0]]) {
					obj[n[0]] = {};
    			 	obj[n[0]][n[1]] = v;
			   } else {
				   if(obj[n[0]] instanceof Array) {
					   if(obj[n[0]][obj[n[0]].length - 1][n[1]]) {
						   var o = {};
						   o[n[1]] = v;
						   obj[n[0]].push(o);
					   } else {
						   obj[n[0]][obj[n[0]].length -1][n[1]] = v;
					   }
				   } else {
					   if(obj[n[0]][n[1]]) {
						   var o = {};
						   o[n[1]] = v;
    					   obj[n[0]] = [obj[n[0]], o];
					   } else {
						   obj[n[0]][n[1]] = v;
					   }
				   }
			   }
		   } else {
			   if(obj[this['name']]) {
				   if(obj[this['name']] instanceof Array) {
					   obj[this['name']].push(v);
				   } else {
					   obj[this['name']] = [obj[this['name']], v];
				   }
			   } else {
				   obj[this['name']] = v;
			   }
		   }
	   });
		return obj;
	};
	// 
	$.ajaxSetup({
	    dataType: 'json',
		contentType:'application/json;charset=UTF-8',
        beforeSend: function (xhr, setting) {
			$('.loading').show();
        },
		dataFilter: function(result) {
			try {
				result = JSON.parse(result);
				if(result.code === '401') {
					if(window.parent) {
    					if(window.parent.parent) {
    						window.parent.parent.document.location.href = '/login.html';
    					} else {
    						window.parent.document.location.href = '/login.html';
    					}
					} else {
						location.href = '/login.html';
					}
		            return null;
		        } else if(result.code === '403') {
		        	Toastr.error(__("you have no permission"));
		            return null;
		        }
				return JSON.stringify(result);
			} catch(e) {
				return result;
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			switch (jqXHR.status) {
			case (404):
				error(__("route not found"));
				break;
			}
		}
    });
	// 
    $('.datetimepicker').each(function(i, d) {
    	$(d).datepicker({
            language:"zh-CN",
            format: $(d).data('date-format'),
            todayBtn: "linked",
            autoclose: true,
            todayHighlight: true
        });
    });
});